package com.cn.controller;

import com.cn.entity.user.User;
import com.cn.service.UserService.DeleteUserService;
import com.cn.service.UserService.InsertUserService;
import com.cn.service.UserService.UpdateUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.cn.service.UserService.LoginService;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName controller01
 * @Description TODO
 * @Author yujiajing
 * @Date 2022/10/2 19:34
 * @Version 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {
    /**
     *
     * @param username 前端传来的用户名
     * @param password  前端传来的密码
     * @param vertificationCode  前端传来的验证码
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login( @Param("username") String username,@Param("password") String password ,@Param("VerificationCode")Integer[]vertificationCode ){
        System.out.println(username);
        System.out.println(password);
        System.out.println(vertificationCode);
        LoginService loginService=new LoginService();
        if(loginService.login(username,password)){
            return "/CartView.jsp";
        }
        else{
            return "login_false";
        }
    }




    @RequestMapping("/insert")
    public String  insert(User user){
      /*  String id=user.getId();
        String username=user.getUsername();
        String password=user.getPassword();*/


        InsertUserService insertUserService=new InsertUserService();
        insertUserService.insert(user);
      return "insert_success";
    }

    @RequestMapping("/delete")
    public  String delete(String id){
        DeleteUserService deleteUserService=new DeleteUserService();
        deleteUserService.delete(id);
        return  "delete_success";
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    @ResponseBody
    public  void deleteById(@RequestBody  String id) {//RequestBody接收后台传来的数据
        System.out.println(id.substring(7));
        String RealId=id.substring(7);
       DeleteUserService deleteUserService=new DeleteUserService();
      deleteUserService.delete(RealId);

    }

    @RequestMapping(value = "/update")
    public String update(User user ){
        String id=user.getId();
        String username=user.getUsername();
        String password=user.getPassword();
        UpdateUserService updateUserService =new UpdateUserService();
        updateUserService.update(username,password,id);
        return "update_success";
    }
  @RequestMapping(value = "/home",method = RequestMethod.GET)
    public  String home(){
        return  "redirect:UserManagement.jsp";
  }


}
